//
//  ReportController.m
//  FinancialProduct
//
//  Created by Stella on 2017/9/7.
//  Copyright © 2017年 maq. All rights reserved.
//
#import "MJRefresh.h"
#import "myWebViewController.h"
#import "HandleNetProduct.h"
#import "TransactionInfoCell.h"
#import "ReportController.h"
#import "CKPuslichTool.h"
#import "SearchController.h"
#import "CKPuslichTool.h"
#import "UserModel.h"

@interface ReportController (){
    int count;
    BOOL isReport;
}
@property (weak, nonatomic) IBOutlet UITableView *mTableView;
@property(strong, nonatomic)NSMutableArray *dataArray;
@property (weak, nonatomic) IBOutlet UIView *noDataView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableTop;
@property (weak, nonatomic) IBOutlet UIButton *searchBt;
@property(strong,nonatomic)NSString *searchNa;
@end

@implementation ReportController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArray=[[NSMutableArray alloc] init];
    
    count=1;
    _searchNa=@"";
    if ([self.title isEqualToString:@"产品报告"]) {
        isReport=YES;
        self.tableTop.constant=0;
    }else{
        isReport=NO;
        self.tableTop.constant=60;
        [self.searchBt setTitle:@"搜索产品" forState:UIControlStateNormal];
        [self.searchBt setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    }
//    [self reportDeatil];
    _mTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        count=1;
        [self reportDeatil];
    }];
    _mTableView.mj_footer =[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        count++;
        [self reportDeatil];
    }];
    
    // 马上进入刷新状态
    [self.mTableView.mj_header beginRefreshing];
    // Do any additional setup after loading the view.
}
-(IBAction)rightButton:(UIButton *)barBt{
    [self.searchBt setTitle:@"搜索产品" forState:UIControlStateNormal];
    [self.searchBt setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    //去搜索页面
    SearchController *vc=(SearchController *)[CKPuslichTool getViewControllerFromStroyBoard:@"My" viewID:@"SearchController"];
    vc.whereFrom=@"Report";
    vc.returnSearchContent = ^(NSString *searchContent) {
        _searchNa=searchContent;
        [self.searchBt setTitle:searchContent forState:UIControlStateNormal];
        [self.searchBt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        count=1;
        [self reportDeatil];
    };
  
    [self.navigationController pushViewController:vc animated:YES];
    
}

-(void)reportDeatil{
    UserModel *model=[UserModel getUser];
    int mid=[model.CustomerID intValue];
    NSDictionary *dic;
    if (isReport) {
        //产品报告
        dic=@{@"AAAMethodName":@"InfodisclosureReportSelect",@"ProductId":_ProductId,
                            @"AttachingName":_searchNa,
                            @"PageIndex":@(count),@"PageSize":@"20"};

    }else{
        //产品公告
        dic=@{@"AAAMethodName":@"InfodisclosureNoticeSelect",@"ProductId":_ProductId,@"CustomerID":@(mid),
              @"InformationTitle":_searchNa,
              @"PageIndex":@(count),@"PageSize":@"20"};
       
    }
  
    [HandleNetProduct getListforParams:dic Success:^(NSArray *data) {
        [self.mTableView.mj_header endRefreshing];
        [self.mTableView.mj_footer endRefreshing];
       
        if (count==1) {
            [self.dataArray removeAllObjects];
        }
        if (data.count>0) {
            
            [self.dataArray addObjectsFromArray:data];
            
        }
        if (self.dataArray.count>0) {
            self.noDataView.hidden=YES;
        }else{
            self.noDataView.hidden=NO;
        }
        [self.mTableView reloadData];

    } Failure:^(NSString *errorInfo) {
        [self.mTableView.mj_header endRefreshing];
        [self.mTableView.mj_footer endRefreshing];
    }];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Table view data source
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.dataArray.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    TransactionInfoCell *cell=[tableView dequeueReusableCellWithIdentifier:@"TransactionInfoCell" forIndexPath:indexPath];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.accessoryType=UITableViewCellAccessoryNone;
    if (isReport) {
        cell.nameLa.text=_dataArray[indexPath.row][@"AttachingName"];
    }else{
        cell.nameLa.text=_dataArray[indexPath.row][@"InformationTitle"];
        
    }

    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    myWebViewController *vc=(myWebViewController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Setting" viewID:@"myWebViewController"];
    vc.title=self.title;
    if (isReport) {
        vc.urlString=_dataArray[indexPath.row][@"path"];
    }else{
        if ([_dataArray[indexPath.row][@"Path"] length]>0) {
            vc.urlString=_dataArray[indexPath.row][@"Path"];
            vc.isPDF=YES;
        }else{
            vc.urlString=_dataArray[indexPath.row][@"InformationDes"];
        }
        
    }

    [self.navigationController pushViewController:vc animated:YES];
}


@end
